package cn.com.yusys.yusp.admin.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.yusys.yusp.admin.domain.AdminSmInstu;
import cn.com.yusys.yusp.admin.service.AdminSmInstuService;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;


/**
 * 
 * @项目名称: yusp-admin
 * @类名称: AdminSmInstuResource
 * @类描述: 金融机构Resource
 * @功能描述: 
 * @创建人: hujun3@yusys.com.cn
 * @创建时间: 2017-12-27 15:12
 * @修改备注: 
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("api/adminsminstu")
public class AdminSmInstuResource extends CommonResource<AdminSmInstu, String> {

	private Logger logger = LoggerFactory.getLogger(AdminSmInstuResource.class);
	@Autowired
	private AdminSmInstuService service;
	
	@Override
	protected CommonService getCommonService() {
		return this.service;
	}
	/**
	 * 
	* @方法名称: getList
	* @方法描述: 分页查询
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/querypage")
	public ResultDto<List<Map<String, Object>>> getList(QueryModel queryModel) {
		List<Map<String, Object>> list = service.queryPage(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list);
	}

	/**
	 * 
	* @方法名称: insertInfo
	* @方法描述: 新增金融机构
	* @参数与返回说明: 
	* @算法描述:
	 */
	@PostMapping("/insertinfo")
		public ResultDto<Object> insertInfo(@RequestBody AdminSmInstu info) {
		Map<String,Object> s = service.insertInfo(info);
		ResultDto<Object> result = new ResultDto<>();
		result.setMessage((String) s.get("message"));
		result.setData(s);
		return result;
	}
	
	/**
	 * 
	* @方法名称: useBatch
	* @方法描述: 批量启用
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/usebatch")
	public ResultDto<Object> useBatch(String ids,String lastChgUsr) {
		int n=0;
		Map<String,Object> result=new HashMap<String, Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
					n=n+this.service.useIngFn(idStr[i],lastChgUsr);
				}
			}
			logger.info("Batch enable financial institutions [PK: {"+ids+"}] "+ DateUtil.formatDateTimeByDef());
		}
		resultdto.setMessage((String) result.get("message"));
		resultdto.setData(result);
		return resultdto;
	}
	
	/**
	 * 
	* @方法名称: unUseBatch
	* @方法描述: 批量停用
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/unusebatch")
	public ResultDto<Object> unUseBatch(String ids,String lastChgUsr) {
		int n=0;
		List<Object> i18nData = new ArrayList<Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			int count = 0;
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
                    int res=this.service.queryOrgByInstuId(idStr[i]);
                    if(res>0){
                        count++;
                    }else{
                        n=n+this.service.unUseIngFn(idStr[i],lastChgUsr);
                    }
				}
			}
            if(count==0) {
            	resultdto.setCode(MessageConstants.RET_CODE_20100013);
            	resultdto.setMessage("成功停用"+n+"条数据!");
            	i18nData.add(n);
//				result.put("code", "200");
//                result.put("message", "成功停用"+n+"条数据!");
            }else {
            	resultdto.setCode(MessageConstants.RET_CODE_20100014);
            	resultdto.setMessage("成功停用"+n+"条数据,有"+count+"条数据由于存在机构数据，无法停用!");
            	i18nData.add(n);
            	i18nData.add(count);
//				result.put("code", "2");
//            	result.put("message", "成功停用"+n+"条数据,有"+count+"条数据由于存在机构数据，无法停用!");
            }
		}
		resultdto.setI18nData(i18nData);
		return resultdto;
	}
	
	/**
	 * 
	* @方法名称: deleteBatch
	* @方法描述: 批量删除
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/deletebatch")
	public ResultDto<Object> deleteBatch(String ids) {
		int n=0;
		List<Object> i18nData = new ArrayList<Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			int nodeletes=0;
			String delete="";
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
					int res=this.service.queryOrgByInstuId(idStr[i]);
					AdminSmInstu adminSmInstu = this.service.selectByPrimaryKey(idStr[i]);
					String sts = adminSmInstu.getInstuSts();
					if(res>0) {
						if("W".equals(sts)) {
							n=n+this.service.deleteOrgAndInstu(idStr[i]);
							delete=delete+idStr[i];
						}else {
							nodeletes++;
						}	
					}else {//金融机构下有机构数据的不让删除
						n=n+this.service.deleteByPrimaryKey(idStr[i]);
						delete=delete+idStr[i];
					}
					
				}
			}
			logger.info("Batch delete financial institutions [PK: {"+delete+"}] "+ DateUtil.formatDateTimeByDef());
			if(nodeletes==0) {
				resultdto.setCode(MessageConstants.RET_CODE_20100015);
				resultdto.setMessage("成功删除"+n+"条数据!");
				i18nData.add(n);
//				result.put("message", "成功删除"+n+"条数据!");
			}else {
				resultdto.setCode(MessageConstants.RET_CODE_20100016);
				resultdto.setMessage("成功删除"+n+"条数据,有"+nodeletes+"条数据由于存在机构数据，无法删除!");
				i18nData.add(n);
				i18nData.add(nodeletes);
//				result.put("message", "成功删除"+n+"条数据,有"+nodeletes+"条数据由于存在机构数据，无法删除!");
			}
		}
		resultdto.setI18nData(i18nData);
//		resultdto.setMessage((String) result.get("message"));
//		resultdto.setData(result);

		return resultdto;
	}
	
}
